package com.lins.framework.utils;


import android.graphics.Bitmap;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 文件的读写
 */
public class StoreUtils {

    /**
     * @param path
     * @param saveObject
     * @return void
     * @throws
     * @Title: saveObject
     * @Description: 保存对象到制定目录
     */
    public static final void saveObject(String path, Object saveObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            File f = new File(path);
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(saveObject);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 保存Bitmap
     *
     * @param path
     * @param photo
     */
    public static void saveBitmap(String path, Bitmap photo) {
        File f = new File(path);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            photo.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @param path
     * @return Object
     * @throws
     * @Title: restoreObject
     * @Description: 从文件中读取对象
     */
    public static final Object restoreObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;
        File f = new File(path);
        if (!f.exists()) {
            return null;
        }
        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
            return object;

        } catch (FileNotFoundException e) {
            //LogWriter.logException(TAG, "", e);
        } catch (IOException e) {
            //LogWriter.logException(TAG, "", e);
        } catch (ClassNotFoundException e) {
            //LogWriter.logException(TAG, "", e);
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                //LogWriter.logException(TAG, "", e);
            }
        }
        return object;
    }


    /**
     * 保存文件文件到目录
     *
     * @return 文件保存的目录
     */
    public static String getSaveFilePath(String filePath, String fileName) {
//
//        filePath = filePath + File.separator + path;
        File file = new File(filePath);
        if (!file.exists()) {
            boolean b = file.mkdirs();
            Log.i("file", "文件不存在  创建文件    " + b);
        }
        String resultPath = filePath + File.separator + fileName;
        return resultPath;
    }
}
